<?php
namespace app\api\controller;

use app\BaseController;
use app\console\model\Config;
use app\model\DeviceRent as SelfModel;
use app\model\LeasingCompany;
use app\model\Project;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use think\facade\Cookie;
use think\Response;
use think\Request;
class DeviceRent extends Base
{


    public function initialize()
    {
        parent::initialize();
        $this->model = "\app\model\DeviceRent";
        $this->list_rows = 10;
        $this->index = ['id','project_title','title','id_no','name_no','price','create_time'];
        $this->all = ['id','company_id','company_title','project_id','project_title','device_id','title','id_no','name_no','price','create_time'];
        $this->my = ['id','company_id','company_title','project_id','project_title','device_id','title','id_no','name_no','price','create_time'];
        $this->view = ['id','company_id','company_title','project_id','project_title','device_id','title','id_no','name_no','price','create_time'];
        $this->msg = '项目租金报表';
        
        
    }

    public function index(Request $request)
    {
        $where = [];
        if(!empty(input('start_time')) && !empty($request->end_time)){
            //$where[] = ['create_time','between',[input('start_time'),$request->end_time]];
        }

        $data = $this->model::withSearch(array_keys($request->data), $request->data)
            ->field($this->index)
//            ->where($where)
            ->order(['id' => 'desc'])
            ->paginate($this->list_rows);
        // echo $this->model::getlastsql();
        return ['code' => 200, 'msg' =>  '项目租金报表', 'data' => $data];
    }

//    public function export(Request $request)
//    {
//
//        $list =  $data = $this->model::withSearch(array_keys($request->data), $request->data)
//            ->field($this->index)
////                ->withoutField($this->without_field)
////                ->where($this->status)
//            ->where($this->where)
//            ->field(['id','title','id_no','name_no','use_status','remote_status','online_status','is_rent_title','rent_start_time','rent_end_time','project_title','remark','start_time','end_time','create_time'])
//            ->order($this->order)
//            ->select();
//
//        $spreadsheet = new Spreadsheet();
////        $spreadsheet->setActiveSheetIndex(0);
//        $sheet = $spreadsheet->getActiveSheet()->setTitle('工时记录');
//        $title_arg = ['ID','序号','铭牌号','设备号','项目名称','启租状态','吊篮状态','远程状态','在线状态','年费开始时间','年费结束时间','年费延迟时间','启租时间','停租时间','备注'];
//        for ($i=1;$i<=count($title_arg);$i++){
//            $sheet->setCellValueByColumnAndRow($i,1,  $title_arg[$i-1]);
//        }
//
//        foreach ($list as $k => $v) {
//            $j = $k + 2;
//            $sheet->setCellValueByColumnAndRow(1, $j, $v['id']);
//            $sheet->setCellValueByColumnAndRow(2, $j, $v['id_no']);
//            $sheet->setCellValueByColumnAndRow(3, $j, $v['name_no']);
//            $sheet->setCellValueByColumnAndRow(4, $j, $v['title']);
//            $sheet->setCellValueByColumnAndRow(5, $j, $v['project_title']);
//            $sheet->setCellValueByColumnAndRow(6, $j, $v['is_rent_title']);
//            $sheet->setCellValueByColumnAndRow(7, $j, $v['use_status']);
//            $sheet->setCellValueByColumnAndRow(8, $j, $v['remote_status']);
//            $sheet->setCellValueByColumnAndRow(9, $j, $v['online_status']);
//            $sheet->setCellValueByColumnAndRow(10, $j, $v['start_time']);
//            $sheet->setCellValueByColumnAndRow(11, $j, $v['end_time']);
//            $sheet->setCellValueByColumnAndRow(12, $j, $v['delay_time']);
//            $sheet->setCellValueByColumnAndRow(13, $j, $v['rent_start_time']);
//            $sheet->setCellValueByColumnAndRow(14, $j, $v['rent_end_time']);
//            $sheet->setCellValueByColumnAndRow(15, $j, $v['remark']);
//
//        }
//
////        $spreadsheet->createSheet()->setTitle('员工总工时');;
////        $spreadsheet->setActiveSheetIndex(1);
////        $sheet = $spreadsheet->getActiveSheet(1);
//
////        $sheet->setCellValue('A1', 'Hello World !');
//        $filename ='设备导出.xlsx';
//        // Redirect output to a client’s web browser (Xls)
//        header('Content-Type: application/vnd.ms-excel');
//        header('Content-Disposition: attachment;filename="'.$filename.'"');
//        header('Cache-Control: max-age=0');
//// If you're serving to IE 9, then the following may be needed
//        header('Cache-Control: max-age=1');
//
//// If you're serving to IE over SSL, then the following may be needed
//        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
//        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
//        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
//        header('Pragma: public'); // HTTP/1.0
//
//        $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
//        $writer->save('php://output');
//        exit;
//
//    }

    public function export(Request $request)
    {

        $list =  $data = $this->model::withSearch(array_keys($request->data), $request->data)
            ->field($this->index)
//                ->withoutField($this->without_field)
//                ->where($this->status)
            ->where($this->where)
            ->field(['id','project_title','title','id_no','name_no','price','create_time'])
//            ->order($this->order)
            ->select();

        $spreadsheet = new Spreadsheet();
//        $spreadsheet->setActiveSheetIndex(0);
        $sheet = $spreadsheet->getActiveSheet()->setTitle('租金报表');
        $title_arg = ['ID','序号','铭牌号','设备号','项目名称','租金支付金额','租金支付日期'];
        for ($i=1;$i<=count($title_arg);$i++){
            $sheet->setCellValueByColumnAndRow($i,1,  $title_arg[$i-1]);
        }

        foreach ($list as $k => $v) {
            $j = $k + 2;
            $sheet->setCellValueByColumnAndRow(1, $j, $v['id']);
            $sheet->setCellValueByColumnAndRow(2, $j, $v['id_no']);
            $sheet->setCellValueByColumnAndRow(3, $j, $v['name_no']);
            $sheet->setCellValueByColumnAndRow(4, $j, $v['title']);
            $sheet->setCellValueByColumnAndRow(5, $j, $v['project_title']);
            $sheet->setCellValueByColumnAndRow(6, $j, $v['price']);
            $sheet->setCellValueByColumnAndRow(7, $j, $v['create_time']);

        }

        $name = '租金报表:';
        $company_name = LeasingCompany::where('id',$request->company_id)->value('title');
        $project_name = Project::where('id',$request->project_id)->value('title');
        if(!empty($company_name)){
            $name .=$company_name;
        }
        if(!empty($project_name)){
            $name .= '-'.$project_name;
        }
        if(!empty($request->start_time)){
            $name .= '-'.$request->start_time;
        }
        if(!empty($request->end_time)){
            $name .= '-'.$request->end_time;
        }
        $writer = new Xlsx($spreadsheet);
//        $name=rand(10,99);
        $writer->save('./excel/'.$name.'.xlsx');
        return ['code'=>200,'msg'=>'租金报表导出','url'=> $request->domain().'/excel/'.$name.'.xlsx'];
    }




}
